const csv = require('csvtojson');
const fs = require('fs');


const metricsTypeArr_zhiKaDingDan = ["normalEntity", "resaleEntity", "superSIMEntity", "normalWhite", "superSIMWhite",
    "halfWhite", "snsdESIM", "snmdESIM"
]

const provinceMap = {
    100: "北京",
    200: "广东",
    431: "吉林",
    451: "黑龙江",
    311: "河北",
    951: "宁夏",
    971: "青海",
    731: "湖南",
}

const typeMap = {
    normalEntity: "普通实体卡",
    resaleEntity: "转售实体卡",
    superSIMEntity: "超级SIM实体卡",
    normalWhite: "普通空白卡",
    superSIMWhite: "超级SIM空白卡",
    halfWhite: "半白卡",
    snsdESIM: "一号一ESIM",
    snmdESIM: "一号双ESIM"
}

async function readDataFromCsv(filePath) {
    return csv().fromFile(filePath)
        .subscribe(json => {
            return new Promise((resolve, reject) => {
                if (!json) {
                    reject('read csv error')
                } else {
                    resolve(json)
                }
            })
        })
}

async function genMapJson() {
    let nodeList = await readDataFromCsv('../node-server-info/en_endnode.csv');
    nodeList.filter(x => {
        if (!nodeId || nodeId ==='') {
            return true;
        } else {
            return nodeId == x.code;
        }
    }).forEach(nodeInfo => {
        let provinceList = nodeInfo.province.split(',');
        provinceList.filter(x => {
            if (!provinceCode || provinceCode == "") {
                return true;
            } else {
                return x == provinceCode;
            }
        }).forEach(p => {
            console.log(nodeInfo.code, p)
            metricsTypeArr_zhiKaDingDan.forEach(orderType => {
                overrideArray.push({
                        matcher: {
                          id: "byName",
                          options: `${p}-${orderType}`
                        },
                        properties: [
                          {
                            id: "displayName",
                            value: `${provinceMap[p]}-${typeMap[orderType]}`
                          }
                        ]
                })
            })
        })
        
    })

    console.log(JSON.stringify(overrideArray))
}

const nodeId = "";
const provinceCode = "";
const overrideArray = new Array();

genMapJson();